package org.bud.excel.expot.reply;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bud.excel.expot.bit.ExportAskBody;
import org.bud.excel.expot.exception.ExcelException;
import org.bud.util.bit.Lists;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author shaun
 * @date 2024/7/18
 * @instruction
 */
public enum POIExport {
    XLS {
        @Override
        Workbook workbook() {
            return new HSSFWorkbook();
        }
    },
    XLSX{
        @Override
        Workbook workbook() {
            return new XSSFWorkbook();
        }
    };
    public String contentType(){
        return "application/vnd.ms-excel";
    }
    abstract Workbook workbook();
    public void exportExcel(OutputStream os,ExportAskBody<?,?>... body){
        try(Workbook workbook = workbook()){
            new WorkbookWriter(workbook, Lists.newArrayList(body)).write();
            workbook.write(os);
        } catch (Exception e) {
            throw new ExcelException(e);
        } finally {
            try {
                os.flush();
                os.close();
            } catch (IOException e) {
                throw new ExcelException(e);
            }
        }
    }

}
